package com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.*;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.service.HouseFinishLossAssessService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @Description: fxfzxczh
 * @Author: lhl
 * @CreateDate: 2023/4/24 9:45
 * 房屋装修损失评估
 */
@RestController
@RequestMapping("/houseFinishDamageAssess")
@Slf4j
@Api(tags = "房屋装修损失评估")
public class HouseFinishLossAssessController {

    @Resource
    HouseFinishLossAssessService service;

    /**
     * 中高档装修房屋数量占比保存/修改
     * @return
     */
    @PostMapping("middleHighRenovationRatioSave")
    public RestResponse middleHighRenovationRatioSave(@RequestBody MiddleHighRenovationRatio middleHighRenovationRatio){
        RestResponse response = null;
        try{
            service.middleHighRenovationRatioSave(middleHighRenovationRatio);
            response = RestResponse.succeed();
            response.setMessage("修改成功");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 中高档装修房屋数量占比查看
     * @return
     */
    @GetMapping("middleHighRenovationRatioView")
    public RestResponse findByXsemId(@RequestParam("xsemId")String xsemId,String type){
        RestResponse response = null;
        try{
            MiddleHighRenovationRatio middleHighRenovationRatio=service.middleHighRenovationRatioView(xsemId,type);
            response = RestResponse.succeed(middleHighRenovationRatio);
            response.setMessage("修改成功");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    /**
     * 规模提示
     * @return
     */
    @GetMapping("/mhFinishHouseScaleReminder")
    public RestResponse mhFinishHouseCount(String ratioType){
        RestResponse response = null;
        try{
            String msg = service.mhFinishHouseScaleReminder(ratioType);
            response = RestResponse.succeed();
            response.setMessage(msg);
        }catch (Exception e){
            String errorMessage = "失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 中高档房屋装修总面积计算
     * @return
     */
    @PostMapping("/mhFinishHouseCount")
    public RestResponse mhFinishHouseCount(@RequestBody Map<String, Object> map){
        RestResponse response = null;
        try{
            service.mhFinishHouseCount(map);
            response = RestResponse.succeed();
            response.setMessage("计算成功");
        }catch (Exception e){
            String errorMessage = "计算失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 通过地震事件id获取中高档房屋装修总面积
     * @return
     */
    @GetMapping("/getMhFinishHouseByXsemId")
    public RestResponse getMhFinishHouseByXsemId(@RequestParam String xsemId){
        RestResponse response = null;
       try{
            response = RestResponse.succeed(service.getMhFinishHouseByXsemId(xsemId));
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 通过地震事件id获取房屋装修破坏损失比
     * @return
     */
    @GetMapping("/getHouseFinishDlRatioByXsemId")
    public RestResponse getHouseFinishDlRatioByXsemId(@RequestParam String xsemId){
        RestResponse response = null;
        try{
            response = RestResponse.succeed(service.getHouseFinishDlRatioByXsemId(xsemId));
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 房屋装修破坏损失比保存
     * @return
     */
    @PostMapping("/houseFinishDlRatioSave")
    public RestResponse houseFinishDlRatioSave(@RequestBody HouseFinishDamageLossRatio entity){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            service.houseFinishDlRatioSave(entity);
            response = RestResponse.succeed(jsonObject);
            response.setMessage("保存成功");
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 房屋装修破坏损失比修改
     * @return
     */
    @PostMapping("/houseFinishDlRatioUpdate")
    public RestResponse houseFinishDlRatioUpdate(@RequestBody HouseFinishDamageLossRatio entity){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            service.houseFinishDlRatioUpdate(entity);
            response = RestResponse.succeed(jsonObject);
            response.setMessage("修改成功");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 通过地震事件id获取修正系数
     * @return
     */
    @GetMapping("/getCorrectionFactorByXsemId")
    public RestResponse getCorrectionFactorByXsemId(@RequestParam String xsemId){
        RestResponse response = null;
        try{
            response = RestResponse.succeed(service.getCorrectionFactorByXsemId(xsemId));
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 保存修正系数
     * @return
     */
    @PostMapping("/saveCorrectionFactor")
    public RestResponse saveCorrectionFactor(@RequestBody CorrectionFactor entity){
        RestResponse response = null;
        try{
            service.saveCorrectionFactor(entity);
            response = RestResponse.succeed();
            response.setMessage("保存成功");
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 修改修正系数
     * @return
     */
    @PostMapping("/updateCorrectionFactor")
    public RestResponse updateCorrectionFactor(@RequestBody CorrectionFactor entity){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            CorrectionFactor correctionFactor=service.updateCorrectionFactor(entity);
            response = RestResponse.succeed(correctionFactor);
            response.setMessage("修改成功");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 各个评估子区房屋装修直接经济损失计算
     * @return
     */
    @PostMapping("/houseFinishEconomicLossCount")
    public RestResponse houseFinishEconomicLossCount(@RequestBody Map<String, Object> map){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            service.houseFinishEconomicLossCount(map);
            response = RestResponse.succeed(jsonObject);
            response.setMessage("计算成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "计算失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 通过地震事件id获取损失详情
     * type 0各个评估子区房屋装修直接经济损失 1各个评估子区和灾区房屋装修直接经济损失
     * @return
     */
    @GetMapping("/getHouseFinishEconomicLossByXsemId")
    public RestResponse getHouseFinishEconomicLossByXsemId(@RequestParam String xsemId,@RequestParam String type){
        RestResponse response = null;
        try{
            response = RestResponse.succeed(service.getHouseFinishEconomicLossByXsemId(xsemId,type));
            response.setMessage("获取成功！");
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 各个评估子区和灾区房屋装修直接经济损失计算
     * @return
     */
    @PostMapping("/disasterAreaHouseFinishEconomicLossCount")
    public RestResponse disasterAreaHouseFinishEconomicLossCount(@RequestBody Map<String, Object> map){
        RestResponse response = null;
        try{
            service.disasterAreaHouseFinishEconomicLossCount(map);
            response = RestResponse.succeed();
            response.setMessage("计算成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "计算失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }
}
